package com.example.task3.controller;

import java.io.*;

import com.example.task3.domain.Cake;
import com.example.task3.service.ShoppingCart;
import com.example.task3.service.ShoppingItem;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

@WebServlet(name = "cartServlet", value = "/cartServlet")
public class CartServlet extends HttpServlet {
    private String message;
    ShoppingCart cart;

    public void init() {
        message = "Hello World!";
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException{
        doPost(request,response);

    }
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        //获取请求参数
        String opt=request.getParameter("opt");
        System.out.println("opt:"+opt);
        //如果参数为show,调用显示购物车的方法showCart()
        if("show".equals(opt)){
            showCart(request,response);
        }
        //如果参数为add，调用加入购物车的方法
        if ("add".equals(opt)){
            addCart(request,response);
        }

    }

    /**
     * 添加商品到购物车
     * @param request
     * @param response
     * @throws IOException
     */
    private void addCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取蛋糕的id
        String id=request.getParameter("id");
        //获取蛋糕的name
        String name=request.getParameter("name");
        //获取蛋糕的price
        double price=Double.parseDouble(request.getParameter("price"));
        //获取蛋糕的imageUrl
        String imageUrl=request.getParameter("imageUrl");
        //获取购买数量
        int num=Integer.parseInt(request.getParameter("num"));
       //创建一个Cake的对象
        Cake cake = new Cake(id,name,price);
        //得到一个HttpSession
        HttpSession session=request.getSession();
        //如果session中"cart"为null，为cart初始化
        if (session.getAttribute("cart")==null) {
            //初始化cart
            cart=new ShoppingCart();
            session.setAttribute("cart",cart);
        }
        else{
            //从session中获取cart
            cart=(ShoppingCart) session.getAttribute("cart");
        }
        //将蛋糕加入购物车
        cart.addItem(cake,num);
        //将cart存入session

        System.out.println("成功添加购物车，购物车商品数量："+cart.getAmount());
        response.sendRedirect("index.jsp");
    }

    /**
     * 显示购物车的内容
     * @param request
     * @param response
     * @throws IOException
     */
    private void showCart(HttpServletRequest request, HttpServletResponse response) throws IOException {
        cart=(ShoppingCart) request.getSession().getAttribute("cart");
        if(cart==null){
            System.out.println("购物车为空");
        }
        else{
            System.out.println("-----------购物车列表--------------");
            int num=1;
            for(ShoppingItem si:cart.getItems()){
                System.out.print("商品"+num++);
                System.out.print("\t名称："+si.getCake().getName());
                System.out.print("\t单价："+si.getCake().getPrice());
                System.out.println("\t数量："+si.getQuantity());
                System.out.println("\t小计："+si.getSubTotal());
            }
            System.out.println("---------------------------------");
            System.out.println("购物车商品总计："+cart.getTotalPrice());
        }
    }
    public void destroy() {
    }
}